package org.gr.ias.financing.controller;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.gr.ias.common.entity.ProjectFinancing;
import org.gr.ias.common.model.JqgridBean;
import org.gr.ias.common.model.PageRusult;
import org.gr.ias.common.util.PropertiesUtil;
import org.gr.ias.financing.service.FinancingService;
import org.gr.ias.financing.util.FinancingEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 融资管理
 */
@Slf4j
@Controller
@RequestMapping(value = "/financing")
public class FinancingController {

    @Resource
    private FinancingService financingService;

    private Logger log = LoggerFactory.getLogger(FinancingController.class);

    @RequestMapping("/financingManager")
    //@RequiresPermissions(value = {"融资管理"})
    public String tousermanage() {
        return "power/financing";
    }

    /*
     *查询融资项目
     */
    @ResponseBody
    @RequestMapping(value = "/selectFinancing")
    public Map<String, Object> selectFinancing(JqgridBean jqgridbean){
        Map<String,Object> map = new HashMap<>();
        log.info("开始查询融资项目列表");
        Example example = new Example(ProjectFinancing.class);
        PageHelper.startPage(jqgridbean.getPage(), jqgridbean.getLength());
        List<ProjectFinancing> projectFinancings = financingService.selectFinancingList();
        PageRusult<ProjectFinancing> pageRusult =new PageRusult<ProjectFinancing>(projectFinancings);
        if (projectFinancings.size()!=0){
            for (ProjectFinancing pro:projectFinancings) {
                int status = Integer.valueOf(pro.getStatus());
                if (FinancingEnum.draft.getCode()==status){
                    pro.setStatus(FinancingEnum.draft.getName());
                }else if (FinancingEnum.published.getCode()==status){
                    pro.setStatus(FinancingEnum.published.getName());
                }else if (FinancingEnum.inreview.getCode()==status){
                    pro.setStatus(FinancingEnum.inreview.getName());
                }else if (FinancingEnum.unapprove.getCode()==status){
                    pro.setStatus(FinancingEnum.unapprove.getName());
                }else if (FinancingEnum.aproved.getCode()==status){
                    pro.setStatus(FinancingEnum.aproved.getName());
                }else if (FinancingEnum.locked.getCode()==status){
                    pro.setStatus(FinancingEnum.locked.getName());
                }else if (FinancingEnum.closed.getCode()==status){
                    pro.setStatus(FinancingEnum.closed.getName());
                }else if (FinancingEnum.lostefficacy.getCode()==status){
                    pro.setStatus(FinancingEnum.lostefficacy.getName());
                }else if (FinancingEnum.blacklist.getCode()==status){
                    pro.setStatus(FinancingEnum.blacklist.getName());
                }
            }
        }else{
            map.put("msg", PropertiesUtil.getValueByName("msg"));
        }
        log.info(projectFinancings.toString());
        log.info("查询融资列表项目完毕");
        map.put("currpage", String.valueOf(pageRusult.getPageNum()));
        map.put("totalpages", String.valueOf(pageRusult.getPages()));
        map.put("totalrecords", String.valueOf(pageRusult.getTotal()));
        map.put("datamap",projectFinancings);
        return map;
    }

    /**
     * 融资项目详情
     */
    @RequestMapping(value = "/financingMsg")
    public Map<String, Object> financingMsg(Integer id){
        Map<String,Object> map = new HashMap<>();
        log.info("开始融资项目详情列表");
        ProjectFinancing projectFinancing = financingService.selectByKey(id);
        log.info("查询融资项目详情完毕");
        map.put("projectFinancing",projectFinancing);
        return map;
    }

    /**
     * 审核融资项目
     */
    @PostMapping(value = "/checkFinancing")
    public Map<String, Object> checkFinancing(ProjectFinancing projectFinancing){
        Map<String,Object> map = new HashMap<>();
        projectFinancing.setApproveUser("admin");
        projectFinancing.setApproveTime(new Date());
        log.info("开始审核融资项目");
        log.info("审核融资项目完毕");
        return map;
    }

    /**
     * 锁定融资项目
     */
    @RequestMapping(value = "/lockFinancing")
    public Map<String, Object> lockFinancing(){
        Map<String,Object> map = new HashMap<>();
        log.info("开始锁定融资项目");
        log.info("锁定融资项目完毕");
        return map;
    }

    /**
     * 加入黑名单
     */
    @RequestMapping(value = "/financingAddBlacklist")
    public Map<String, Object> financingAddBlacklist(){
        Map<String,Object> map = new HashMap<>();
        log.info("开始执行加入黑名单操作");
        log.info("执行加入黑名单操作完毕");
        return map;
    }
}
